package com.sy.service.allocate.impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sy.bean.allocate.HomeInventory;
import com.sy.bean.allocate.Inventory;
import com.sy.bean.allocate.InventorySave;
import com.sy.bean.allocate.Stock;
import com.sy.bean.pucharse.excel.InventoryExc;
import com.sy.common.bean.PageBean;
import com.sy.common.crud.BaseServiceImpl;
import com.sy.mapper.allocate.InventoryMapper;
import com.sy.service.allocate.InventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.Map;

@Service
public class InventoryServiceImpl extends BaseServiceImpl<Inventory, InventoryMapper> implements InventoryService {
    @Autowired
    InventoryMapper inventoryMapper;
    @Autowired
    HttpServletRequest request;

    @Override
    public int batchSave(List<InventorySave> inventorySaveList) {
        return inventoryMapper.batchSave(inventorySaveList);
    }

    @Override
    public String exportInv(Map map) {
        List<InventoryExc> list = inventoryMapper.exportInv(map);
        String root = request.getServletContext().getRealPath("/") + "/excel";
        File file = new File(root);
        if (!file.exists())
            file.mkdirs();
        String newFileName = file.getAbsolutePath() + "/盘点记录.xlsx";
        EasyExcel.write(newFileName, InventoryExc.class)
                .sheet("盘点记录")
                .doWrite(() -> {
                    return list;
                });
        return "/excel/盘点记录.xlsx";
    }
    @Override
    public PageBean queryStock(Map map) {
        map.putIfAbsent("page", 1);
        map.putIfAbsent("pageSize", 10);
        int page = Integer.parseInt(map.get("page").toString());
        int pageSize = Integer.parseInt(map.get("pageSize").toString());
        if (page==0){
            page=1;
        }
        if (pageSize==0){
            pageSize=10;
        }
        PageHelper.startPage(page, pageSize);
        List list = inventoryMapper.queryStock(map);
        PageInfo info = new PageInfo(list);
        return new PageBean<>(page, pageSize, info.getTotal(), list);
    }

    @Override
    public HomeInventory queryHomeInventory(int eid) {
        return inventoryMapper.queryHomeInventory(eid);
    }
}
